package com.ruoyi.system.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.system.domain.Dingdan;
import com.ruoyi.system.mapper.YonghuMapper;
import com.ruoyi.system.domain.Yonghu;
import com.ruoyi.system.service.IYonghuService;

/**
 * 用户管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-08-10
 */
@Service
public class YonghuServiceImpl implements IYonghuService 
{
    @Autowired
    private YonghuMapper yonghuMapper;

    /**
     * 查询用户管理
     * 
     * @param yhId 用户管理主键
     * @return 用户管理
     */
    @Override
    public Yonghu selectYonghuByYhId(Long yhId)
    {
        return yonghuMapper.selectYonghuByYhId(yhId);
    }

    /**
     * 查询用户管理列表
     * 
     * @param yonghu 用户管理
     * @return 用户管理
     */
    @Override
    public List<Yonghu> selectYonghuList(Yonghu yonghu)
    {
        return yonghuMapper.selectYonghuList(yonghu);
    }

    /**
     * 新增用户管理
     * 
     * @param yonghu 用户管理
     * @return 结果
     */
    @Transactional
    @Override
    public int insertYonghu(Yonghu yonghu)
    {
        int rows = yonghuMapper.insertYonghu(yonghu);
        insertDingdan(yonghu);
        return rows;
    }

    /**
     * 修改用户管理
     * 
     * @param yonghu 用户管理
     * @return 结果
     */
    @Transactional
    @Override
    public int updateYonghu(Yonghu yonghu)
    {
        yonghuMapper.deleteDingdanByDdYhId(yonghu.getYhId());
        insertDingdan(yonghu);
        return yonghuMapper.updateYonghu(yonghu);
    }

    @Override
    public int modifyStatus(Yonghu yonghu) {
        return yonghuMapper.modifyStatus(yonghu);
    }

    /**
     * 批量删除用户管理
     * 
     * @param yhIds 需要删除的用户管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteYonghuByYhIds(Long[] yhIds)
    {
        yonghuMapper.deleteDingdanByDdYhIds(yhIds);
        return yonghuMapper.deleteYonghuByYhIds(yhIds);
    }

    /**
     * 删除用户管理信息
     * 
     * @param yhId 用户管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteYonghuByYhId(Long yhId)
    {
        yonghuMapper.deleteDingdanByDdYhId(yhId);
        return yonghuMapper.deleteYonghuByYhId(yhId);
    }

    /**
     * 新增订单信息
     * 
     * @param yonghu 用户管理对象
     */
    public void insertDingdan(Yonghu yonghu)
    {
        List<Dingdan> dingdanList = yonghu.getDingdanList();
        Long yhId = yonghu.getYhId();
        if (StringUtils.isNotNull(dingdanList))
        {
            List<Dingdan> list = new ArrayList<Dingdan>();
            for (Dingdan dingdan : dingdanList)
            {
                dingdan.setDdYhId(yhId);
                list.add(dingdan);
            }
            if (list.size() > 0)
            {
                yonghuMapper.batchDingdan(list);
            }
        }
    }
}
